﻿using System;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using System.Windows.Forms;


namespace CodeFactory
{
    public partial class CSharpSourceDesign : ContentWindow
    {
        public CSharpSourceDesign()
        {
            InitializeComponent();
            txtCSharpDesign.SetHighlighting("C#");
        }

        public void SetText(string code)
        {
            txtCSharpDesign.Text = code;
        }

        public void CleanText()
        {
            txtCSharpDesign.Text = "";
        }
        /// <summary>Performs an action encapsulated in IEditAction.</summary>
        /// <remarks>
        /// There is an implementation of IEditAction for every action that 
        /// the user can invoke using a shortcut key (arrow keys, Ctrl+X, etc.)
        /// The editor control doesn't provide a public funciton to perform one
        /// of these actions directly, so I wrote DoEditAction() based on the
        /// code in TextArea.ExecuteDialogKey(). You can call ExecuteDialogKey
        /// directly, but it is more fragile because it takes a Keys value (e.g.
        /// Keys.Left) instead of the action to perform.
        /// <para/>
        /// Clipboard commands could also be done by calling methods in
        /// editor.ActiveTextAreaControl.TextArea.ClipboardHandler.
        /// </remarks>
        private void DoEditAction(TextEditorControl editor, ICSharpCode.TextEditor.Actions.IEditAction action)
        {
            if (editor != null && action != null)
            {
                var area = editor.ActiveTextAreaControl.TextArea;
                editor.BeginUpdate();
                try
                {
                    lock (editor.Document)
                    {
                        action.Execute(area);
                        if (area.SelectionManager.HasSomethingSelected && area.AutoClearSelection /*&& caretchanged*/)
                        {
                            if (area.Document.TextEditorProperties.DocumentSelectionMode == DocumentSelectionMode.Normal)
                            {
                                area.SelectionManager.ClearSelection();
                            }
                        }
                    }
                }
                finally
                {
                    editor.EndUpdate();
                    area.Caret.UpdateCaretPosition();
                }
            }
        }


        private bool HaveSelection()
        {
            var editor = txtCSharpDesign;
            return editor != null &&
                editor.ActiveTextAreaControl.TextArea.SelectionManager.HasSomethingSelected;
        }

        /// <summary>
        /// copy
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (HaveSelection())
                DoEditAction(txtCSharpDesign, new ICSharpCode.TextEditor.Actions.Copy());
        }

        /// <summary>
        /// paste
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DoEditAction(txtCSharpDesign, new ICSharpCode.TextEditor.Actions.Paste());
        }

        /// <summary>
        /// cut
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (HaveSelection())
                DoEditAction(txtCSharpDesign, new ICSharpCode.TextEditor.Actions.Cut());
        }

        /// <summary>
        /// copy to clipboard
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnCopyToClipboard_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(txtCSharpDesign.Text, true);
        }

        /// <summary>
        /// undo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnUndo_Click(object sender, EventArgs e)
        {
            txtCSharpDesign.Undo();
        }

        /// <summary>
        /// redo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnRedo_Click(object sender, EventArgs e)
        {
            txtCSharpDesign.Redo();
        }

    }
}
